﻿using System;
using System.Collections.Generic;
using System.IO;
using Umbraco.Core.IO;
using umbraco.cms.businesslogic.template;

namespace Vega.USiteBuilder.TemplateBuilder
{
    public class TemplateComparer
    {
        public List<ContentComparison> PreviewTemplateChanges()
        {
            var tt = Util.GetFirstLevelSubTypes(typeof (UmbracoTemplatePageBase));
            
            var arg = PreviewTemplates(typeof(TemplateBase));
            return arg;
        }

        public List<ContentComparison> PreviewTemplates(Type typeBaseTemplate)
        {
            List<ContentComparison> templateComparison = new List<ContentComparison>();
            TemplateManager templateManager = new TemplateManager();

            foreach (Type typeTemplate in Util.GetFirstLevelSubTypes(typeBaseTemplate))
            {
                if (typeTemplate.IsGenericType)
                {
                    templateComparison.AddRange(PreviewTemplates(typeTemplate));
                    continue;
                }
                
                string alias = TemplateManager.GetTemplateAlias(typeTemplate);
                Template template = Template.GetByAlias(alias);
                if (template == null)
                {
                    string path =
                        IOHelper.MapPath(SystemDirectories.Masterpages + "/" + alias.Replace(" ", "") + ".master");

                    if (File.Exists(path))
                    {
                        string parentAlias = templateManager.GetParentMasterPageName(File.ReadAllText(path));

                        templateComparison.Add(new ContentComparison
                                                   {
                                                       Alias = alias,
                                                       DocumentTypeStatus = Status.New,
                                                       ParentAlias = parentAlias ?? ""
                                                   });
                    }
                }
                else
                {
                    int parentTemplateId = 0;
                    string parentMasterPageName = templateManager.GetParentMasterPageName(template);
                    if (!string.IsNullOrEmpty(parentMasterPageName) && parentMasterPageName != "default")
                    {
                        Template parentTemplate = Template.GetByAlias(parentMasterPageName);
                        if (parentTemplate == null)
                        {
                            throw new Exception(
                                string.Format(
                                    "Template '{0}' is using '{1}' as a parent template (defined in MasterPageFile in {0}.master) but '{1}' template cannot be found",
                                    template.Alias, parentMasterPageName));
                        }
                        parentTemplateId = parentTemplate.Id;
                    }
                    if (template.MasterTemplate != parentTemplateId)
                    {
                        templateComparison.Add(new ContentComparison { Alias = alias, DocumentTypeStatus = Status.Changed, ParentAlias = parentMasterPageName ?? "default" });

                    }
                    else
                    {
                        templateComparison.Add(new ContentComparison { Alias = alias, DocumentTypeStatus = Status.Same, ParentAlias = parentMasterPageName ?? "default" });
                    }

                }

                // sync all children templates
                templateComparison.AddRange(PreviewTemplates(typeTemplate));
            }

            return templateComparison;
        }
    }
}
